package com.yifeng.repo.micro.service.server.engine.configure;

import com.yifeng.repo.micro.service.server.engine.dal.tx.ForceProxyTransactionManagementConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;

/**
 * Created by daibing on 2023/6/20.
 */
@Slf4j
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableConfigurationProperties(ForceTransactionProperties.class)
@ConditionalOnProperty(prefix = ForceTransactionProperties.PREFIX, name = "enabled", matchIfMissing = true)
public class ForceTransactionConfiguration {

    @Bean
    public static BeanFactoryPostProcessor forceTransactionBeanFactoryPostProcessor() {
        return beanFactory -> {
            BeanDefinition definition = beanFactory.getBeanDefinition(ProxyTransactionManagementConfiguration.class.getName());
            definition.setBeanClassName(ForceProxyTransactionManagementConfiguration.class.getName());
        };
    }
}
